package com.al.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 之前我们做的那个登录页面，业务逻辑处理也在jsp中
 * 但是jsp是为了和也面结合，没有页面就没有必要用jsp
 * 所以我们在这里直接用 servlet 处理登录请求
 * */

public class LoginServlet extends HttpServlet {
	private String myUserName;
	private String myPassword;
	
	@Override
		public void init(ServletConfig config) throws ServletException {
			myUserName = config.getInitParameter("userName");
			myPassword = config.getInitParameter("password");
		}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String userName = req.getParameter("userName");
		String password = req.getParameter("password");
		if(userName.equals(myUserName) && password.equals(myPassword)) {
			//登录成功
			HttpSession session = req.getSession();
			session.setAttribute("userName", userName);
			req.getRequestDispatcher("welcom.jsp").forward(req, resp);
		} else {
			//登录失败
			resp.sendRedirect("login.jsp");
		}
	}
}